/********************************************************************
  Copyright: 2011 Alexander Sokoloff <sokoloff.a@gmail.ru>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License.
  version 2 as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*********************************************************************/

#include "playlists.h"
#include <QDebug>
#include <QtCore/QSettings>
#include <QtCore/QStringList>


/************************************************

 ************************************************/
StationInfo::StationInfo()
{
}


/************************************************

 ************************************************/
StationInfo::~StationInfo()
{
}



/************************************************

 ************************************************/
PlayList::PlayList(const QString &fileName):
    QList<StationInfo*>()
{
    mFileName = fileName;
}


/************************************************

 ************************************************/
PlayList::~PlayList()
{
    qDeleteAll(begin(), end());
}


/************************************************

 ************************************************/
StationInfo* PlayList::addStationInfo(const QString& url, const QString& title)
{
    StationInfo* si = new StationInfo;
    si->setUrl(url);
    si->setTitle(title);
    this->append(si);
    return si;
}

/************************************************

 ************************************************/
PlayList* loadZ(const QString& fileName)
{
    QString f=fileName.toUpper();

/*    if (f.endsWith("PLS"))
    {
        PlayList* res = new PlsPlayList(fileName);
        if (res->doLoad())
            return res;
        else
            delete res;
    }
*/
    return 0;
}


/************************************************

 ************************************************/
PlsPlayList::PlsPlayList(const QString &fileName):
    PlayList(fileName)
{
}


/************************************************

 ************************************************/
bool PlsPlayList::doLoad()
{
    QSettings s(mFileName, QSettings::IniFormat);
    s.setIniCodec("UTF-8");

    s.beginGroup("playlist");
    int cnt = s.allKeys().length() / 2;
    for (int i=1; i<cnt; ++i)
    {
        if (s.contains(QString("File%1").arg(i)))
        {
//            qDebug() << "Add station" <<
//                s.value(QString("File%1").arg(i)).toString() <<
//                s.value(QString("Title%1").arg(i)).toString();

            addStationInfo(s.value(QString("File%1").arg(i)).toString(),
                           s.value(QString("Title%1").arg(i)).toString()
                          );
        }
    }

    return s.status() == QSettings::NoError;
}
